package com.thinkfit.jy.dialog;

import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.common.lib.activity.BaseActivity;
import com.jakewharton.rxbinding3.widget.RxTextView;
import com.thinkfit.jy.R;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
 * 登录弹窗
 */
public class LoginPopupWindow extends PopupWindow implements View.OnClickListener {

    private Activity mActivity;

    private Disposable mDisposableCountDown;
    private OnLoginListener mOnLoginListener;

    private void startTimer() {
        if (mDisposableCountDown != null) {
            mDisposableCountDown.dispose();
        }
        mDisposableCountDown = Observable.intervalRange(1, 60, 0, 1, TimeUnit.SECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Long>() {
                    @Override
                    public void accept(Long aLong) throws Exception {
                        TextView tv = getContentView().findViewById(R.id.tvGetCode);
                        if (aLong == 60) {
                            tv.setText(mActivity.getString(R.string.app_resend));
                            tv.setEnabled(true);
                        } else {
                            tv.setText(mActivity.getString(R.string.app_xxx_second_get, String.valueOf(60 - aLong)));
                            tv.setEnabled(false);
                        }
                    }
                });
    }


    public LoginPopupWindow(Activity activity, View anchor, OnLoginListener listener) {
        super(LayoutInflater.from(activity).inflate(R.layout.activity_login, null),
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
        mActivity = activity;
        mOnLoginListener = listener;
        setTouchable(true);
        setFocusable(true);
        setOutsideTouchable(true);
        setBackgroundDrawable(new ColorDrawable(0));
        update();
        backgroundAlpha(0.4f);
        setAnimationStyle(android.R.style.Animation_Translucent);
        setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                backgroundAlpha(1f);
                if (mDisposableCountDown != null) {
                    mDisposableCountDown.dispose();
                }
            }
        });
        showAsDropDown(anchor);
        getContentView().findViewById(R.id.tvWechat).setOnClickListener(this);
        getContentView().findViewById(R.id.tvGetCode).setOnClickListener(this);
        getContentView().findViewById(R.id.tvLogin).setOnClickListener(this);
        initInputListener();
    }

    private void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
        lp.alpha = bgAlpha;
        mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        mActivity.getWindow().setAttributes(lp);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tvWechat:
                getContentView().findViewById(R.id.llPhoneLogin).setVisibility(View.GONE);
                getContentView().findViewById(R.id.llWechatLogin).setVisibility(View.VISIBLE);
                break;
            case R.id.tvGetCode:
                String phone = ((EditText) getContentView().findViewById(R.id.etPhone)).getText().toString().trim();
                if (TextUtils.isEmpty(phone)) {
                    ((BaseActivity) mActivity).showToast(R.string.app_input_phone);
                    return;
                }
                if (mOnLoginListener != null) {
                    mOnLoginListener.sendCode(phone);
                }
                v.setEnabled(false);
                startTimer();
                break;
            case R.id.tvLogin:
                phone = ((EditText) getContentView().findViewById(R.id.etPhone)).getText().toString().trim();
                String code = ((EditText) getContentView().findViewById(R.id.etVerificationCode)).getText().toString().trim();
                if (mOnLoginListener != null) {
                    mOnLoginListener.telLogin(phone, code);
                }
                break;
        }
    }

    private void initInputListener() {
        final TextView tvLogin = getContentView().findViewById(R.id.tvLogin);
        tvLogin.setEnabled(false);
        final EditText etPhone = getContentView().findViewById(R.id.etPhone);
        final EditText etVerificationCode = getContentView().findViewById(R.id.etVerificationCode);
        Observable.combineLatest(RxTextView.textChanges(etPhone).skip(1),
                RxTextView.textChanges(etVerificationCode).skip(1),
                (charSequence, charSequence2) -> !TextUtils.isEmpty(etPhone.getText().toString().trim()) &&
                        !TextUtils.isEmpty(etVerificationCode.getText().toString().trim())).subscribe(aBoolean -> {
            if (aBoolean) {
                tvLogin.setBackgroundResource(R.drawable.shape_ffffff_5);
                tvLogin.setTextColor(ContextCompat.getColor(mActivity, com.common.lib.R.color.color_21_20_20));
                tvLogin.setEnabled(true);
            } else {
                tvLogin.setBackgroundResource(R.drawable.shape_2bffffff_5);
                tvLogin.setTextColor(ContextCompat.getColor(mActivity, com.common.lib.R.color.color_2b_fe_fe_fe));
                tvLogin.setEnabled(false);
            }
        });
    }

    public interface OnLoginListener {
        public void sendCode(String phone);

        public void telLogin(String phone, String code);
    }
}
